package com.goods.business.service.imp;

import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.model.business.Health;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@SuppressWarnings("all")
public class HealthServiceImpl implements HealthService {

    @Autowired
    private HealthMapper healthMapper;

    @Override
    public Boolean report(Health health, Long userId) {

        //将当前时间转换成"yyyy-MM-dd"格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String todayTime =simpleDateFormat.format(new Date());

        //查询当前用户的打卡记录
        //封装查询条件example
        Example example = new Example(Health.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        //查询出当前登录用户的所有打卡记录
        List<Health> healthInfoList = healthMapper.selectByExample(example);
        //获取到最近一条打卡记录
        Health h = healthInfoList.get(healthInfoList.size() - 1);
        //获取当前登录用户最近一条打卡记录的打卡时间
        Date createTime = h.getCreateTime();
        //转换成字符串
        String createTimeStr = simpleDateFormat.format(createTime);

        //如果最近一条打卡记录的时间和当前的时间相等进行打卡操作并返回true，否则返回false
        if (!todayTime.equals(createTimeStr)){
            health.setUserId(userId);
            health.setCreateTime(new Date());
            int insert = healthMapper.insert(health);
            return true;
        }else {
            return false;
        }

    }

    @Override
    public Health isReport(Long userId) {
        //将当前时间转换成"yyyy-MM-dd"格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String todayTime =simpleDateFormat.format(new Date());
        //查询当前用户的打卡记录
        //封装查询条件example
        Example example = new Example(Health.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);

        //查询出当前登录用户的所有打卡记录
        List<Health> healthInfoList = healthMapper.selectByExample(example);
        //获取到最近一条打卡记录
        Health h = healthInfoList.get(healthInfoList.size() - 1);
        //获取当前登录用户最近一条打卡记录的打卡时间
        Date createTime = h.getCreateTime();
        //转换成字符串
        String createTimeStr = simpleDateFormat.format(createTime);

        //如果最近一条打卡记录的时间和当前的时间相等进行打卡操作并返回最近的一条打卡记录回显到前端，否则返回控制
        if (todayTime.equals(createTimeStr)){
            return h;
        }else {
            return null;
        }

    }

    @Override
    public List<Health> history(Long userId) {

        //查询当前用户的打卡记录
        //封装查询条件example
        Example example = new Example(Health.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        List<Health> healthInfoList = healthMapper.selectByExample(example);
        //返回当前用户的打卡记录
        return healthInfoList;
    }
}
